const config = require('../config/default')
const jwt = require('jsonwebtoken')
module.exports = {
    // 签发token
    setToken: (username,userid) => {
        return new Promise((resolve, reject) => {
            const token = jwt.sign({
                username: username,
                _id: userid
            }, config.token_key, { expiresIn: '7d' });
            resolve(token)
        }).catch((err) => {
          console.log('setToken ERROR:'+err)
      })
    },
    // 解析token
    verToken: (token) => {
        return new Promise((resolve, reject) => {
            var userInfo = jwt.verify(token.split(' ')[1], config.token_key);
            resolve(userInfo)
        }).catch((err => {
          console.log('verToken ERROR:'+err)
      }))
    }
}